#ifndef __LOG_HPP__
#define __LOG_HPP__

#include <cstdio>
#include <cstring>
#include <ctime>
#include <cstdarg>

#define DEBUG   0
#define NORMAL  1
#define WARNING 2
#define ERROR   3
#define FATAL   4

const char* g_str[]={
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

#define FILE_NAME "./log.txt"

void logMessage(int level,const char* format,...)
{
    //命令行-D 定义了DEBUG，那么就打印Debug信息，否则不打印
#ifndef DEBUG1
    if(level==0) return;
#endif
    //标准
    char stdBuff[1024];
    memset(stdBuff,0,sizeof(stdBuff));
    time_t tm=time(nullptr);
    snprintf(stdBuff,sizeof(stdBuff),"[%s] [%d] ",g_str[level],tm);

    //自定义
    char logBuff[1024];
    memset(logBuff,0,sizeof(logBuff));
    va_list args;
    va_start(args,format);
    vsnprintf(logBuff,sizeof(logBuff),format,args);
    va_end(args);

    //命令行-D 定义了PRINT就往显示器上打印，否则想文件内打印
#ifdef PRINT
    printf("%s%s\n",stdBuff,logBuff);
#else
    FILE* fp=fopen(FILE_NAME,"a");
    if(fp==nullptr)
    {
        perror("fopen");
    }

    fprintf(fp,"%s%s\n",stdBuff,logBuff);
    fclose(fp);
#endif
}

#endif